/*
 * MIT License
 *
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import { Service } from './Service'
import { ble } from '@kit.ConnectivityKit';
import { ValuesBucket } from '@kit.ArkData';

export class BleDevice {
  private id: string;

  private name: string;

  private services: Service[];

  public clientDevice: ble.GattClientDevice;

  public mtu: number = 23;

  constructor(id: string, name: string) {
    this.id = id;
    this.name = name;
  }

  public getId(): string {
    return this.id;
  }

  public setId(id: string) {
    this.id = id;
  }

  public getName(): string {
    return this.name;
  }

  public setName(name: string) {
    this.name = name;
  }

  public getServices(): Service[] {
    return this.services;
  }

  public setServices(services: Service[]) {
    this.services = services;
  }

  public getServiceByUUID(uuid: string): Service | null {
    if (this.services == null) {
      return null;
    }

    var result: Service = null;
    this.services.forEach(value => {
      if (value.getUuid() == uuid) {
        result = value;
      }
    });
    return result;
  }

  public asJSObject(rssi?: number): ValuesBucket {
    return {
      'id': this.getId(),
      'name': this.getName(),
      'rssi': rssi,
      'mtu': this.mtu,
      'manufacturerData': null,
      'serviceData': null,
      'serviceUUIDs': null,
      'localName': null,
      'txPowerLevel': null,
      'solicitedServiceUUIDs': null,
      'isConnectable': null,
      'overflowServiceUUIDs': null
    }
  }
}